package your.rpg.namespace;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.content.Context;
import android.content.res.AssetManager;

public class LevelFileReader {
	private Context context;
	private LevelLoader levelloader;
	
	public LevelFileReader(Context context1,LevelLoader loader){
		context = context1;
		levelloader = loader;
		levelloader.setFileReader(this);
	}
	
	public void readFileLevel(String filename, int id) throws IOException{
		AssetManager am = context.getAssets();
		InputStream is = am.open(filename + ".txt");
		DataInputStream in = new DataInputStream(is);
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		String strLine;
		//variables to use
		int background = 0;
		String levelname = "";
		int move1 = -1;
		int move2 = -1;
		int move3 = -1;
		String point1 = "";
		String point2 = "";
		String point3 = "";
		while ((strLine = br.readLine()) != null)   {
			if(strLine.contains("background:"))
			{
				background = Integer.parseInt(strLine.substring(12));  
			}
			else if(strLine.contains("levelname:"))
			{
				levelname = strLine.substring(12);
			}
			else if(strLine.contains("move1:") && !strLine.contains("null"))
			{
				move1 = Integer.parseInt(strLine.substring(12));
			}
			else if(strLine.contains("move2:") && !strLine.contains("null"))
			{
				move2 = Integer.parseInt(strLine.substring(12));
			}
			else if(strLine.contains("move3:") && !strLine.contains("null"))
			{
				move3 = Integer.parseInt(strLine.substring(12));
			}
			else if(strLine.contains("point1:") && !strLine.contains("null"))
			{
				point1 = strLine.substring(12);
			}
			else if(strLine.contains("point2:") && !strLine.contains("null"))
			{
				point2 = strLine.substring(12);
			}
			else if(strLine.contains("point3:") && !strLine.contains("null"))
			{
				point3 = strLine.substring(12);
			}
		}
		levelloader.addLevel(id, background, levelname, move1, move2, move3, point1, point2, point3);
	}

	public void readFileObject(String filename, int id) throws IOException{
		AssetManager am = context.getAssets();
		InputStream is = am.open(filename + ".txt");
		DataInputStream in = new DataInputStream(is);
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		String strLine;
		//variables to use
		int image = 0;
		int xpos = 0;
		int ypos = 0;
		String name = "";
		while ((strLine = br.readLine()) != null)   {
			if(strLine.contains("object:"))
			{
				image = Integer.parseInt(strLine.substring(8, 18));
				xpos = Integer.parseInt(strLine.substring(19, 22));
				ypos = Integer.parseInt(strLine.substring(23, 26));
				name = strLine.substring(27);
				levelloader.addObject(id, name, image, xpos, ypos);
			}
		}
	}

	public void readFileEvent(String filename, int id) throws IOException{
		AssetManager am = context.getAssets();
		InputStream is = am.open(filename + ".txt");
		DataInputStream in = new DataInputStream(is);
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		String strLine;
		//variables to use
		String listname = "";
		String action = "";
		while ((strLine = br.readLine()) != null)   {
			listname = strLine.substring(0,11);
			if(listname.contains("point"))
			{
				listname += strLine.substring(11, 12);
			}
			action = strLine.substring(16);
			levelloader.addEvent(id, listname, action);
		}
	}
}
